<?php
	session_start();
    
    require_once($to_root.'_classes/Main.config.php');
    $GLOBALS['start_time'] = microtime(true);
    
    //Rechte
    if(isset($authorized) && is_array($authorized)) {
        $gr = (isset($_SESSION['User']['group'])) ? $_SESSION['User']['group'] : -1;
        if(!in_array($gr, $authorized) && !isset($GLOBALS['ajax'])) {
        	header('Location: '.$GLOBALS['config']['login']);
        }
    }
    
	$GLOBALS['Messages'] = array();    
    
	//Base Classes
    require_once($GLOBALS['config']['root'].'_classes/Service.php');
    require_once($GLOBALS['config']['root'].'_classes/IService.php');
    require_once($GLOBALS['config']['root'].'_classes/DatabaseInterface.php');
    require_once($GLOBALS['config']['root'].'_classes/Template/Template.php');
	require_once($GLOBALS['config']['root'].'_classes/User/User.php');
	require_once($GLOBALS['config']['root'].'_classes/Template/ViewDescriptor.php');
	require_once($GLOBALS['config']['root'].'_classes/Template/SubViewDescriptor.php');
    //Wrapper Classes
    require_once($GLOBALS['config']['root'].'_classes/wrapper/Message.php');
	require_once($GLOBALS['config']['root'].'_classes/wrapper/Todo.php');
    require_once($GLOBALS['config']['root'].'_classes/wrapper/Project.php');
    require_once($GLOBALS['config']['root'].'_classes/wrapper/UserDescription.php');
	
    //Main Localization
	require_once($GLOBALS['config']['root'].'_localization/core/main.loc.php');
	
	//GLOBALS Classes
	$GLOBALS['Template'] = new Template();
	$GLOBALS['User'] = new User();
	$GLOBALS['Db-I'] = new DatabaseInterface();
	
	//Error Messages per $_GET
	$errors = array('insert_success'=>$GLOBALS['Localization']['Db']['INSERT_SUCCESS'][$GLOBALS['Localization']['language']],
					'insert_error'=>$GLOBALS['Localization']['Db']['INSERT_ERROR'][$GLOBALS['Localization']['language']],
					'update_success'=>$GLOBALS['Localization']['Db']['UPDATE_SUCCESS'][$GLOBALS['Localization']['language']],
					'update_error'=>$GLOBALS['Localization']['Db']['UPDATE_ERROR'][$GLOBALS['Localization']['language']],
					'delete_success'=>$GLOBALS['Localization']['Db']['DELETE_SUCCESS'][$GLOBALS['Localization']['language']],
					'delete_error'=>$GLOBALS['Localization']['Db']['DELETE_ERROR'][$GLOBALS['Localization']['language']],
					'not_authorized'=>$GLOBALS['Localization']['User']['NOT_AUTHORIZED'][$GLOBALS['Localization']['language']],
					'logout_success'=>$GLOBALS['Localization']['User']['LOGOUT_SUCCESS'][$GLOBALS['Localization']['language']],
					'logout_error'=>$GLOBALS['Localization']['User']['LOGOUT_ERROR'][$GLOBALS['Localization']['language']],
					'logout_forced'=>$GLOBALS['Localization']['User']['LOGOUT_FORCED'][$GLOBALS['Localization']['language']],
					'nick_used'=>$GLOBALS['Localization']['User']['NICK_ALREADY_TAKEN'][$GLOBALS['Localization']['language']],
					'not_found'=>$GLOBALS['Localization']['Db']['NOT_FOUND'][$GLOBALS['Localization']['language']],
					'choose_project'=>$GLOBALS['Localization']['Todo']['CHOOSE_PROJECT'][$GLOBALS['Localization']['language']],
					'share_success'=>$GLOBALS['Localization']['Project']['SHARE_SUCCESS'][$GLOBALS['Localization']['language']],
					'share_error'=>$GLOBALS['Localization']['Project']['SHARE_ERROR'][$GLOBALS['Localization']['language']],
					'unshare_success'=>$GLOBALS['Localization']['Project']['UNSHARE_SUCCESS'][$GLOBALS['Localization']['language']],
					'unshare_error'=>$GLOBALS['Localization']['Project']['UNSHARE_ERROR'][$GLOBALS['Localization']['language']]); // errors per $_GET
	
	$error_class = array('insert_success'=>'info',
						'insert_error'=>'error',
						'update_success'=>'info',
						'update_error'=>'error',
						'delete_success'=>'info',
						'delete_error'=>'error',
						'not_authorized'=>'error',
						'not_authorized'=>'error',
						'logout_success'=>'success',
						'logout_error'=>'error',
						'logout_forced'=>'error',
						'nick_used'=>'error',
						'not_found'=>'error',
						'choose_project'=>'error',
						'share_success'=>'info',
						'share_error'=>'error',
						'unshare_success'=>'info',
						'unshare_error'=>'error');	
	
	//Check if current Session is Valid (if User is logged in)
	if(isset($_SESSION['User'])){
		if($GLOBALS['Db-I']->checkIfValidSession() !== true) {
			if(isset($GLOBALS['ajax'])) {
				echo 'Error'; break; exit(0);
			}else{ 
				if(!isset($_GLOBALS['ajax'])) header('Location: logout.php?action=force');
			}
		}
		if(!isset($_SESSION['ajax_sID'])) $_SESSION['ajax_sID'] = md5(microtime(true));
	}
?>